package com.cyzy.util;

import com.cyzy.utils.RedisUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cuncun
 * @create 2025-05-22 下午3:21
 */
//消费者接收数据
@RocketMQMessageListener(topic = "rocket-send1",consumerGroup ="consumer-demo1")
@Component
public class RocketConsumerListener implements RocketMQListener<Map> {


    @Autowired
    private AliyunSmsUtil aliyunSmsUtil;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void onMessage(Map map) {
        System.out.println("consumer 收到消息：" + map);

        try {
            // 发送短信
            boolean success = aliyunSmsUtil.sendSms(map);

            //如果发送成功把验证码存入reids中
            if(success){
                System.out.println((String) map.get("phone"));
                System.out.println((String) map.get("code"));
                //存储token到redis
                if(map.get("phone") != null && map.get("code") != null){
                    redisUtil.set((String) map.get("phone"), (String) map.get("code"));
                    redisUtil.setExpire((String) map.get("phone"),180);
                }else {
                    System.out.println("短信发送失败或参数为空，未写入 Redis");
                }

            }
        } catch (Exception e) {
            System.out.println("发送失败");
            throw new RuntimeException(e);
        }

    }
}
